package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.query.VideoAuthQueryObject;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.JSONResultUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 视频认证
 */
@Controller
public class VideoAuthController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private IOrderTimeService orderTimeService;

    @Autowired
    private IVideoAuthService videoAuthService;

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 视频认证页面
     */
    @NeedLogin
    @RequestMapping("videoAuditOrder")
    public String videoAuditOrder(Model model, @ModelAttribute("qo")VideoAuthQueryObject qo){
        LoginInfo loginInfo = UserContext.getLoginInfo();
        UserInfo userInfo = userInfoService.getUserInfo(loginInfo.getId());
        //是否已经通过了视频认证
        if(userInfo.hasVedioAuth()){
            model.addAttribute("videoSuccess",true);
            return "videoOrder";
        }
        //是否未通过视频认证,但是预约了视频认证
        if(userInfo.getVideoAuthId()!= null){
            model.addAttribute("videoAuditOrder",videoAuthService.getVideoAuth(userInfo.getVideoAuthId()));
            return "videoOrder";
        }
        //查询客服
        model.addAttribute("auditors",loginInfoService.listAll());
        //查询可预约的日期
        List<Date> dateList = new ArrayList<>();
        Date now = new Date();
        Date tomorrow = DateUtils.addDays(now,1);
        Date afterTomorrow = DateUtils.addDays(tomorrow,1);
        dateList.add(now);
        dateList.add(tomorrow);
        dateList.add(afterTomorrow);
        model.addAttribute("orderDates",dateList);
        //查询可预约时间
        //model.addAttribute("orderTimes",orderTimeService.listAll());
        return "videoOrder";
    }

    /**
     * 视频认证预约申请
     * @param videoAuth
     * @return
     */
    @NeedLogin
    @RequestMapping("/saveVideoAuditOrder")
    @ResponseBody
    public Object order(VideoAuth videoAuth) {
        videoAuthService.order(videoAuth);
        return JSONResultUtil.jsonResult();
    }

    /**
     * 取消视频认证预约申请
     */
    @NeedLogin
    @RequestMapping("/updateVideoAuditOrderState")
    @ResponseBody
    public Object cancel(Long id){
        videoAuthService.cancel(id);
        return JSONResultUtil.jsonResult();
    }

    /**
     * 可预约视频认证申请
     */
    @NeedLogin
    @RequestMapping("/availableTime")
    @ResponseBody
    public Object availableTime(@ModelAttribute("qo")VideoAuthQueryObject qo){
        List availableTimes = orderTimeService.getAvailableTime(qo);
        return availableTimes;
    }



}
